﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

public partial class Admin_account_request : System.Web.UI.Page
{
    myDataSetTableAdapters.AccountsTableAdapter accAdapter = new myDataSetTableAdapters.AccountsTableAdapter();
    protected void Page_Load(object sender, EventArgs e)
    {
        txtId.Enabled = false;
        txtUser.Enabled = false;
        txtFullname.Enabled = false;
        txtRollNo.Enabled = false;
        RadDatePicker1.Enabled = false;
        if (!IsPostBack)
        {
            Panel1.Visible = false;
            display();
        }
    }

    protected void display()
    {
        myDataSet.AccountsDataTable accTable = accAdapter.GetDataConvertByStatusType();
        GridView1.DataSource = accTable;
        GridView1.DataBind();
    }

    protected void clear()
    {
        txtId.Text = "";
        txtUser.Text = "";
        txtFullname.Text = "";
        txtRollNo.Text = "";
        RadDatePicker1.SelectedDate = null;
        DropDownList1.SelectedValue = "-1";
        lblUserTypeError.Visible = false;
    }

    protected bool checkEmpty()
    {
        bool status = true;
        if (DropDownList1.SelectedValue == "-1" || DropDownList1.SelectedValue == "0")
        {
            lblUserTypeError.Visible = true;
            status = false;
        }
        else
        {
            lblUserTypeError.Visible = false;
        }
        return status;
    }

    protected void cmdAccept_Click(object sender, EventArgs e)
    {
        if (checkEmpty())
        {
            accAdapter.UpdateQuery(txtFullname.Text.Trim(), txtRollNo.Text.Trim(), RadDatePicker1.SelectedDate.Value, true, int.Parse(DropDownList1.SelectedValue.ToString()), int.Parse(txtId.Text.Trim()));
            display();
            Panel1.Visible = false;
        }
    }

    protected void cmdDeny_Click(object sender, EventArgs e)
    {
        try
        {
            int id = int.Parse(txtId.Text.Trim());
            accAdapter.DeleteQuery(id);
            clear();
            display();
            Panel1.Visible = false;
        }
        catch
        {
            RegisterStartupScript("as", "<script>alert('Cannot delete!')</script>");
        }
    }

    protected void getDataById(int id)
    {
        myDataSet.AccountsDataTable accTable = accAdapter.GetDataById(id);
        txtId.Text = accTable[0]["id"].ToString();
        txtUser.Text = accTable[0]["userName"].ToString();
        txtFullname.Text = accTable[0]["fullName"].ToString();
        txtRollNo.Text = accTable[0]["rollNo"].ToString();
        RadDatePicker1.SelectedDate = DateTime.Parse(accTable.Rows[0]["admissionDate"].ToString());
        DropDownList1.SelectedValue = accTable[0]["userType"].ToString();
    }

    protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
    {
        int id = int.Parse(GridView1.DataKeys[e.NewEditIndex].Value.ToString());
        Panel1.Visible = true;
        clear();
        getDataById(id);
    }

    protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        try
        {
            int id = int.Parse(GridView1.DataKeys[e.RowIndex].Value.ToString());
            accAdapter.DeleteQuery(id);
            clear();
            display();
        }
        catch
        {
            RegisterStartupScript("as", "<script>alert('Cannot delete!')</script>");
        }
    }
    protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        GridView1.PageIndex = e.NewPageIndex;
        display();
    }
}
